home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
IRIX 6.2 Applications 1996 May
/
SGI IRIX 6.2 Applications 1996 May.iso
/
dist
/
impr_dev.idb
/
usr
/
impressario
/
src
/
gui_models
/
laserjetPJL
/
util.c.z
/
util.c
Wrap
C/C++ Source or Header
|
1996-05-06
|
41KB
|
1,505 lines
/**************************************************************************
*
* Copyright (c) 1993 Silicon Graphics, Inc.
*
* Permission to use, copy, modify, distribute, and sell this software and
* its documentation for any purpose is hereby granted without fee, provided
* that (i) the above copyright notices and this permission notice appear in
* all copies of the software and related documentation, and (ii) the name of
* Silicon Graphics may not be used in any advertising or publicity relating
* to the software without the specific, prior written permission of Silicon
* Graphics.
*
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
*
* IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
* INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
* RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF
* THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT
* OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* SILICON GRAPHICS RESERVES THE RIGHT TO CHANGE FUTURE VERSIONS OF THIS
* SOFTWARE IN ANY MANNER AND AT ANY TIME WITHOUT PRIOR NOTICE.
*
**************************************************************************
*
* File: util.c
*
* Note to Developer: This file requires no modification.
*
* Description: Utility functions for the graphical model file program.
*
**************************************************************************/
#ident "$Revision: 1.7 $"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <ctype.h>
#include <sys/types.h>
#include <Xm/Xm.h>
#include <X11/StringDefs.h>
#include <Xm/Form.h>
#include <Xm/RowColumn.h>
#include <Xm/PushB.h>
#include <Xm/Label.h>
#include <Xm/Text.h>
#include <Xm/TextF.h>
#include <Xm/ToggleB.h>
#include <Xm/BulletinB.h>
#include "xutil.h"
#include "util.h"
/* File specific layout constants */
#define OPTIONS_INDENT 5
#define TEXT_FIELD_MAR 2
/* No resource defaults */
#define DEF_INSENSITIVE_BACK "gray72"
#define DEF_INSENSITIVE_FORE "gray50"
/* Text field color struct */
typedef struct {
Pixel foreground; /* Foreground color */
Pixel background; /* Background color */
Pixel tshadow; /* Top shadow color */
Pixel bshadow; /* Bottom shadow color */
} GUIColor;
/* Program scope globals */
char *progName;
AppData gmodelResources;
PDInfoStruct *podInfo;
static Widget helpPane = NULL; /* Help pane */
/* Gmodel specific resource list */
static XtResource resources[] = {
/* Insensitive background */
{
GmdlNinsensitiveBackground, GmdlCInsensitiveBackground,
XtRPixel, sizeof(Pixel),
XtOffsetOf(AppData, insensitiveBack), XtRString,
(XtPointer)DEF_INSENSITIVE_BACK
},
/* Insensitive foreground */
{
GmdlNinsensitiveForeground, GmdlCInsensitiveForeground,
XtRPixel, sizeof(Pixel),
XtOffsetOf(AppData, insensitiveFore), XtRString,
(XtPointer)DEF_INSENSITIVE_FORE
},
};
/**************************************************************************
*
* Function: createHelpWindow
*
* Description: Creates a small window for help text to be displayed
* in. Help text changes as user moves his cursor over fields on
* the more options screen.
*
* Parameters:
* widget (I) - any widget
*
* Return: none
*
**************************************************************************/
Widget createHelpWindow(Widget parent)
{
char *str;
Pixel background;
DEFARGS(5);
str=GetResourceString(parent,"DisplayHelpString",
(char*) XtName(parent),"");
if ( str && !strncasecmp(str,"True",4) ) {
STARTARGS;
SETARG(XmNbackground, &background);
XtGetValues(parent, ARGLIST);
helpPane = XmCreateText(parent, "helpPane", NULL, 0);
XtVaSetValues(helpPane,
XmNalignment, XmALIGNMENT_BEGINNING,
XmNeditable, False,
XmNborderWidth, 1,
XmNshadowThickness, 1,
XmNbackground, background,
XmNeditMode, XmMULTI_LINE_EDIT,
XmNwordWrap, True,
XmNrows, 3,
NULL);
XtManageChild(helpPane);
str=
GetResourceString(helpPane,"helpString",(char*) XtName(helpPane),"");
XtAddEventHandler(helpPane,
(EventMask) (EnterWindowMask|LeaveWindowMask),
False, (XtEventHandler) HelpCB, str);
}
return helpPane;
}
/**************************************************************************
*
* Function: getAppResources
*
* Description: Reads the gmodel specific resources fromthe app-defaults
* file. The function fills in the fields in the gmodelResources
* structure which is globally available to all program modules.
*
* Parameters:
* widget (I) - any widget
*
* Return: none
*
**************************************************************************/
void getAppResources(Widget widget)
{
XtGetApplicationResources(widget, (XtPointer)&gmodelResources,
resources, XtNumber(resources), NULL, 0);
}
/**************************************************************************
*
* Function: makeArgs
*
* Description: Constructs argv list from the specified string of options.
*
* Parameters:
* str (I) - string of options to parse
* argcp (O) - number of arguments parsed from str
* argvp (O) - list of options parsed from str
*
* Return: none
*
**************************************************************************/
void makeArgs(const char *str, int *argcp, char ***argvp)
{
register char *s, *sptr, *aptr;
char **av;
register int ag;
/*
* Initialize everything
*/
*argcp = ag = 0;
*argvp = av = NULL;
/*
* Don't do anything if there is nothing in the string
*/
if (!str || *str == '\0')
return;
/*
* We will be modifying the string so make a copy to preserve the
* user's string.
*/
s = sptr = XtNewString(str);
/*
* Build the argument list
*/
while ((aptr = strtok(sptr, " \t")) != NULL) {
sptr = NULL;
av = (av) ? (char**)XtRealloc((char*)av, (ag+1) * sizeof(char*)):
(char**)XtMalloc(sizeof(char*));
av[ag++] = XtNewString(aptr);
}
/*
* Free the temporary string
*/
XtFree((char*)s);
/*
* Set the user variables and return
*/
*argcp = ag;
*argvp = av;
}
/**************************************************************************
*
* Function: freeArgs
*
* Description: Releases the storage allocated by makeArgs. Can also
* be used to free the storage allocated when building any list
* of strings where both the strings and the list of pointers
* have been dynamically allocated.
*
* Parameters:
* argc (I) - number of arguments in list
* argv (I) - argument list to deallocate
*
* Return: none
*
**************************************************************************/
void freeArgs(int argc, char **argv)
{
register int i;
if (!argv)
return;
for (i = 0; i < argc; i++)
XtFree((char*)argv[i]);
XtFree((char*)argv);
}
/**************************************************************************
*
* Function: createOptionPanel
*
* Description: Sets up a generic panel for option switches.
*
* Parameters:
* name (I) - instance name for option panel
* parent (I) - parent widget ID
*
* Return: Manager widget ID for option objects.
*
**************************************************************************/
Widget createOptionPanel(char *name, Widget parent)
{
Widget form, lbl, bb;
char *subName;
char *str;
DEFARGS(15);
/*
* Allocate space for instance names.
*/
subName = (char*)XtMalloc(strlen(name) + 15);
/*
* Create a form to hold everything
*/
sprintf(subName, "%sForm", name);
form = XtCreateManagedWidget(subName, xmFormWidgetClass, parent, NULL, 0);
/*
* Create the title label for the panel
*/
STARTARGS;
SETARG(XmNtopAttachment, XmATTACH_FORM);
SETARG(XmNleftAttachment, XmATTACH_FORM);
lbl = XtCreateManagedWidget(name, xmLabelWidgetClass, form, ARGLIST);
/*
* Add a callback to display a short help message whenever the
* pointer is focused on the widget.
*/
if (helpPane != NULL) {
str=GetResourceString(lbl,"helpString",(char*) XtName(lbl),"");
XtAddEventHandler(lbl, (EventMask) (EnterWindowMask|LeaveWindowMask),
False, (XtEventHandler) HelpCB, str);
}
/*
* Create a bulletin board. Into this will be placed a form
* that will actually hold the option objects. The bulleting board
* allows us to indent the option switches a fixed amount
*/
sprintf(subName, "%sBB", name);
STARTARGS;
SETARG(XmNtopAttachment, XmATTACH_WIDGET);
SETARG(XmNtopWidget, lbl);
SETARG(XmNtopOffset, 4);
SETARG(XmNleftAttachment, XmATTACH_FORM);
SETARG(XmNrightAttachment, XmATTACH_FORM);
SETARG(XmNbottomAttachment, XmATTACH_FORM);
SETARG(XmNmarginHeight, 2);
SETARG(XmNmarginWidth, 2);
bb = XtCreateManagedWidget(subName, xmBulletinBoardWidgetClass,
form, ARGLIST);
/*
* Create the option object form
*/
sprintf(subName, "%sOptForm", name);
form = XtCreateManagedWidget(subName, xmFormWidgetClass, bb, NULL, 0);
/*
* Free name storage
*/
XtFree(subName);
return form;
}
/**************************************************************************
*
* Function: makeTextF
*
* Description: Instantiates a text field
*
* Parameters:
* name (I) - name for text field
* parent (I) - parent widget ID
* cols (I) - number of columns
* maxLen (I) - max number of characters to allow. Unlimited if 0
* specified.
*
* Return: Widget ID of newly created text field
*
**************************************************************************/
Widget makeTextF(char *name, Widget parent, int cols, int maxLen)
{
DEFARGS(10);
STARTARGS;
SETARG(XmNcolumns, cols);
SETARG(XmNmarginHeight, TEXT_FIELD_MAR);
SETARG(XmNmarginWidth, TEXT_FIELD_MAR);
if (maxLen) {
SETARG(XmNmaxLength, maxLen);
}
return XtCreateManagedWidget(name, xmTextFieldWidgetClass,
parent, ARGLIST);
}
/**************************************************************************
*
* Function: makeRadioB
*
* Description: Creates a set of horiontally oriented radio buttons.
*
* Parameters:
* name (I) - name for radio button Row Column
* parent (I) - parent widget ID
* callback (I) - callback procedure
* blistp (O) - array of buttons created
* label... (I) - button instance name. Last argument must
* be NULL.
*
* Return: Widget ID of Row Column containing the radio buttons.
*
**************************************************************************/
/* VARARGS */
Widget makeRadioB(char *name, Widget parent, XtCallbackProc callback,
WidgetList *blistp, ...)
{
Widget widget, bw;
va_list labels;
char *lptr;
register int button = 0;
DEFARGS(10);
/*
* Create the radio Row Column
*/
STARTARGS;
SETARG(XmNmarginHeight, 0);
SETARG(XmNmarginWidth, 0);
SETARG(XmNorientation, XmHORIZONTAL);
SETARG(XmNpacking, XmPACK_TIGHT);
SETARG(XmNspacing, 2 * LABEL_FIELD_SPACE);
SETARG(XmNentryClass, xmToggleButtonWidgetClass);
widget = XmCreateRadioBox(parent, name, ARGLIST);
XtManageChild(widget);
/*
* Create each radio button instance
*/
va_start(labels, blistp);
STARTARGS;
SETARG(XmNmarginHeight, 0);
SETARG(XmNmarginWidth, 0);
while ((lptr = (char*)va_arg(labels, char*)) != NULL) {
bw = XtCreateManagedWidget(lptr, xmToggleButtonWidgetClass,
widget, ARGLIST);
if (callback)
XtAddCallback(bw, XmNvalueChangedCallback, callback,
(XtPointer)button);
button++;
}
va_end(labels);
/*
* If we asked for list of buttons, provide it
*/
if (blistp) {
STARTARGS;
SETARG(XmNchildren, blistp);
XtGetValues(widget, ARGLIST);
}
return widget;
}
/**************************************************************************
*
* Function: makeCheckB
*
* Description: Creates a set of horiontally oriented check buttons.
*
* Parameters:
* name (I) - name for check button Row Column
* parent (I) - parent widget ID
* callback (I) - callback procedure
* blistp (O) - array of buttons created
* label... (I) - button instance name. Last argument must
* be NULL.
*
* Return: Widget ID of Row Column containing the check buttons.
*
**************************************************************************/
/* VARARGS */
Widget makeCheckB(char *name, Widget parent, XtCallbackProc callback,
WidgetList *blistp, ...)
{
Widget widget, bw;
va_list labels;
char *lptr;
register int button = 0;
DEFARGS(10);
/*
* Create the check box Row Column
*/
STARTARGS;
SETARG(XmNmarginHeight, 0);
SETARG(XmNmarginWidth, 0);
SETARG(XmNorientation, XmHORIZONTAL);
SETARG(XmNpacking, XmPACK_TIGHT);
SETARG(XmNspacing, 2 * LABEL_FIELD_SPACE);
SETARG(XmNentryClass, xmToggleButtonWidgetClass);
SETARG(XmNradioBehavior, False);
SETARG(XmNradioAlwaysOne, False);
widget = XmCreateRadioBox(parent, name, ARGLIST);
XtManageChild(widget);
/*
* Create each radio button instance
*/
va_start(labels, blistp);
STARTARGS;
SETARG(XmNmarginHeight, 0);
SETARG(XmNmarginWidth, 0);
SETARG(XmNindicatorType, XmN_OF_MANY);
while ((lptr = (char*)va_arg(labels, char*)) != NULL) {
bw = XtCreateManagedWidget(lptr, xmToggleButtonWidgetClass,
widget, ARGLIST);
if (callback)
XtAddCallback(bw, XmNvalueChangedCallback, callback,
(XtPointer)button);
button++;
}
va_end(labels);
/*
* If we asked for list of buttons, provide it
*/
if (blistp) {
STARTARGS;
SETARG(XmNchildren, blistp);
XtGetValues(widget, ARGLIST);
}
return widget;
}
/**************************************************************************
*
* Function: makeOptionB
*
* Description: Creates an option button and its menu.
*
* Parameters:
* name (I) - name for option Row Column
* parent (I) - parent widget ID
* callback (I) - callback procedure
* blistp (O) - array of buttons created
* labels (I) - menu item labels
* numLabels (I) - number of labels
* incr (I) - amount to increment through the label list. This allows us
* to pass arrays of structures and access the string
* members.
*
* Return: Widget ID of Row Column containing the option button.
*
******************************************************* *******************/
Widget makeOptionB(char *name, Widget parent, XtCallbackProc callback,
WidgetList *blistp, char **labels,
register int numLabels, register int incr)
{
Widget menuPane, optionRC, optionLabel, bw;
register int i, offset;
register int button = 0;
DEFARGS(5);
/*
* Create the menu pane
*/
menuPane = XmCreatePulldownMenu(parent, "optionPane", NULL, 0);
/*
* Create menu buttons
*/
for (i = 0, offset = 0; i < numLabels; i++, offset += incr) {
bw = XtCreateManagedWidget(*(char**)((paddr_t)labels+offset),
xmPushButtonWidgetClass, menuPane, NULL, 0);
if (callback)
XtAddCallback(bw, XmNactivateCallback, callback,
(XtPointer)button);
button++;
}
/*
* Create the option button RowColumn
*/
STARTARGS;
SETARG(XmNsubMenuId, menuPane);
optionRC = XmCreateOptionMenu(parent, name, ARGLIST);
XtManageChild(optionRC);
if ((optionLabel = XtNameToWidget(optionRC, "OptionLabel")) != NULL)
XtUnmanageChild(optionLabel);
/*
* If we asked for list of buttons, provide it
*/
if (blistp) {
STARTARGS;
SETARG(XmNchildren, blistp);
XtGetValues(menuPane, ARGLIST);
}
return optionRC;
}
/**************************************************************************
*
* Function: makeLabeledRC
*
* Description: Creates a RowColumn with a left side label. This function
* assumes that the RowColumn's parent is a Form widget. The label
* widget created will be named <name> and the RowColumn will be
* name <name>RC.
*
* Parameters:
* name (I) - name for label of RowColumn. Name of RowColumn itself
* is <name>RC.
* parent (I) - parent widget ID
* topWidget (I) - top widget to which the RowColumn should be attached.
* If NULL is specified the widget is attached to
* the top of the form.
*
* Return: Widget ID of RowColumn.
*
**************************************************************************/
Widget makeLabeledRC(char *name, Widget parent, Widget topWidget)
{
Widget rc;
char *rcName;
DEFARGS(15);
/*
* Form the option RowColumn instance name
*/
rcName = (char*)XtMalloc(strlen(name) + 10);
sprintf(rcName, "%sRC", name);
/*
* Create the option RowColumn
*/
STARTARGS;
SETARG(XmNorientation, XmHORIZONTAL);
SETARG(XmNpacking, XmPACK_TIGHT);
SETARG(XmNspacing, LABEL_FIELD_SPACE);
SETARG(XmNmarginWidth, 0);
SETARG(XmNmarginHeight, 1);
SETARG(XmNleftAttachment, XmATTACH_FORM);
if (topWidget) {
SETARG(XmNtopAttachment, XmATTACH_WIDGET);
SETARG(XmNtopWidget, topWidget);
} else {
SETARG(XmNtopAttachment, XmATTACH_FORM);
}
rc = XtCreateManagedWidget(rcName, xmRowColumnWidgetClass,
parent, ARGLIST);
/*
* Free allocated resources
*/
XtFree(rcName);
/*
* Create the option label. Note that we put OPT_TITLE_ID in the
* userData resource of the title label. This is so that when we
* call alignOptions the label is detected as the title.
*/
STARTARGS;
SETARG(XmNuserData, (XtPointer)OPT_TITLE_ID);
XtCreateManagedWidget(name, xmLabelWidgetClass, rc, ARGLIST);
return rc;
}
/**************************************************************************
*
* Function: makeLabeledForm
*
* Description: Creates a Form with a left side label. This function
* assumes that the RowColumn's parent is a Form widget. The label
* widget created will be named <name> and the Form will be
* name <name>Form.
*
* Parameters:
* name (I) - name for label of Form. Name of Form itself is <name>Form.
* parent (I) - parent widget ID
* topWidget (I) - top widget to which the Form should be attached.
* If NULL is specified the widget is attached to
* the top of the form.
* labelWidgetp (O) - label widget created for the form.
*
* Return: Widget ID of Form.
*
**************************************************************************/
Widget makeLabeledForm(char *name, Widget parent, Widget topWidget,
Widget *labelWidgetp)
{
Widget form;
char *formName;
DEFARGS(15);
/*
* Form the option Form instance name
*/
formName = (char*)XtMalloc(strlen(name) + 10);
sprintf(formName, "%sForm", name);
/*
* Create the option Form
*/
STARTARGS;
SETARG(XmNmarginWidth, 0);
SETARG(XmNleftAttachment, XmATTACH_FORM);
if (topWidget) {
SETARG(XmNtopAttachment, XmATTACH_WIDGET);
SETARG(XmNtopWidget, topWidget);
} else {
SETARG(XmNtopAttachment, XmATTACH_FORM);
}
form = XtCreateManagedWidget(formName, xmFormWidgetClass, parent, ARGLIST);
/*
* Free allocated resources
*/
XtFree(formName);
/*
* Create the option label. Note that we put OPT_TITLE_ID in the
* userData resource of the title label. This is so that when we
* call alignOptions the label is detected as the title.
*/
STARTARGS;
SETARG(XmNuserData, (XtPointer)OPT_TITLE_ID);
*labelWidgetp = XtCreateManagedWidget(name, xmLabelWidgetClass,
form, ARGLIST);
return form;
}
/**************************************************************************
*
* Function: HelpCB
*
* Description: Callback to display help text.
*
* Return: none
*
**************************************************************************/
void HelpCB(Widget w, char *clientData, XEvent *event)
{
switch(event->type)
{
case EnterNotify:
XtVaSetValues(helpPane, XmNvalue, clientData, NULL);
break;
case LeaveNotify:
/*
XtVaSetValues(helpPane, XmNvalue, "", NULL);
*/
break;
}
}
/**************************************************************************
*
* Function: alignOptions
*
* Description: Aligns options such that their labels are right justified.
* The alignment is performed by obtaining the children of the
* specified parent widget. If is assumed that the parent widget is
* a Form and the children are manager widgets that have been left
* attached using the offset attachment constraint. The function looks
* for a title label child in each option manager widget. The title
* label is identified by having its user data resource set to
* OPT_TITLE_ID. If this label is not found, the first label found will
* be considered the title label.
*
* Parameters:
* parentForm (I) - widget ID of Form parent
*
* Return: none
*
**************************************************************************/
void alignOptions(Widget parentForm)
{
WidgetList optionMgrs, optionItems;
Widget label;
Widget action;
char *str;
register int i, j;
int numMgrs, numItems, titleID;
Dimension *widths, height;
Dimension maxWidth = 0;
static XmFontList fontlist;
DEFARGS(10);
/*
* If first time through, create a dummy label so we can get its
* fontlist.
*/
if (!fontlist) {
Widget lbl = XtCreateWidget("dummy", xmLabelWidgetClass,
parentForm, NULL, 0);
STARTARGS;
SETARG(XmNfontList, &fontlist);
XtGetValues(lbl, ARGLIST);
XtDestroyWidget(lbl);
}
/*
* Get the manager children of the parent Form
*/
STARTARGS;
SETARG(XmNnumChildren, &numMgrs);
SETARG(XmNchildren, &optionMgrs);
XtGetValues(parentForm, ARGLIST);
/*
* Allocate an array for the label widths
*/
widths = (Dimension*)XtMalloc(numMgrs * sizeof(Dimension));
/*
* Cycle through each option manager and find its label widget.
*/
for (i = 0; i < numMgrs; i++) {
/*
* If this child is not a manager, forget it
*/
if (XtIsComposite(optionMgrs[i]) == False)
continue;
/*
* Get option items
*/
STARTARGS;
SETARG(XmNnumChildren, &numItems);
SETARG(XmNchildren, &optionItems);
XtGetValues(optionMgrs[i], ARGLIST);
/*
* Find label widget
*/
label = NULL;
for (j = 0; j < numItems; j++) {
if (XtIsSubclass(optionItems[j], xmLabelWidgetClass)) {
if (!label)
label = optionItems[j];
STARTARGS;
SETARG(XmNuserData, &titleID);
XtGetValues(optionItems[j], ARGLIST);
if (titleID == OPT_TITLE_ID) {
label = optionItems[j];
break;
}
}
}
if (!label)
continue;
/*
* Get the width of the title and update the maximum
* width value.
*/
STARTARGS;
SETARG(XmNlabelString, &str);
XtGetValues(label, ARGLIST);
XmStringExtent(fontlist, str, &widths[i], &height);
if (widths[i] > maxWidth)
maxWidth = widths[i];
XtFree(str);
/*
* Add a callback to display a short help message whenever the
* pointer is focused on the widget.
*/
if (helpPane != NULL) {
str=GetResourceString(label,"helpString",(char*) XtName(label),"");
XtAddEventHandler(optionMgrs[i], (EventMask)
(EnterWindowMask|LeaveWindowMask),
False, (XtEventHandler) HelpCB, str);
}
}
/*
* Now align the options
*/
for (i = 0; i < numMgrs; i++) {
/*
* If this child is not a manager, forget it
*/
if (XtIsComposite(optionMgrs[i]) == False)
continue;
/*
* Set the left offset
*/
STARTARGS;
SETARG(XmNleftOffset, OPTIONS_INDENT + maxWidth - widths[i]);
XtSetValues(optionMgrs[i], ARGLIST);
}
/*
* Free the width array
*/
XtFree((char*)widths);
}
/**************************************************************************
*
* Function: loadList
*
* Description: Loads the specified scrolled list object with the specified
* list of strings.
*
* Parameters:
* widget (I) - ID of a list widget
* items (I) - list items
* numItems (I) - number of items
* incr (I) - amount to increment through the label list. This allows us
* to pass arrays of structures and access the string
* members.
*
* Return: none
*
**************************************************************************/
void loadList(Widget widget, char **items, register int numItems,
register int incr)
{
register int i, offset;
XmString *listItems;
DEFARGS(5);
/*
* Convert the list of strings into a list of Motif
* compound strings
*/
listItems = (XmString*)XtMalloc(numItems * sizeof(XmString));
for (i = 0, offset = 0; i < numItems; i++, offset += incr)
listItems[i] = XmStringCreateSimple(*(char**)((paddr_t)items+offset));
/*
* Set the compound string list into the list widget
*/
STARTARGS;
SETARG(XmNitems, listItems);
SETARG(XmNitemCount, numItems);
XtSetValues(widget, ARGLIST);
/*
* Free all allocated storage
*/
for (i = 0; i < numItems; i++)
XmStringFree(listItems[i]);
XtFree((char*)listItems);
}
/**************************************************************************
*
* Function: getLabelFontlist
*
* Description: Returns the fontlist to use for label widgets that are
* not major titles. We get the toggle button font and return that
* as the proper font for minor object labels. Minor object labels
* are things like unit labels (in., cm., %, etc.).
*
* Parameters:
* w (I) - any instantiated manager widget ID
*
* Return: Fontlist for the label
*
**************************************************************************/
XmFontList getLabelFontlist(Widget w)
{
static XmFontList fontlist = NULL;
if (!fontlist) {
DEFARGS(5);
Widget dummy = XtCreateWidget("dummy", xmPushButtonWidgetClass,
w, NULL, 0);
STARTARGS;
SETARG(XmNfontList, &fontlist);
XtGetValues(dummy, ARGLIST);
XtDestroyWidget(dummy);
}
return fontlist;
}
/**************************************************************************
*
* Function: intVerifyCB
*
* Description: Used as a verify called for text fields which accept
* integers. The routine will disallow everything except
* an empty text field, the digits 0-9 or if the specified range
* permits, '-' and '+'. The numerical value of the input is not
* checked against the specified range. This is done using the
* intValueCB. If the range structure pointer is NULL, no sign
* checking is performed.
*
* Parameters:
* w (I) - widget which triggered this callback
* clientData (I) - min/max value struct
* callData (I) - text verify callback
*
* Return: none
*
**************************************************************************/
/* ARGSUSED */
void intVerifyCB(Widget w, XtPointer clientData, XtPointer callData)
{
register int i, len;
register char ch;
char *newStr;
Position pos;
XmTextVerifyCallbackStruct *verify = (XmTextVerifyCallbackStruct*)callData;
Irange *range = (Irange*)clientData;
/*
* Assume allowed
*/
verify->doit = True;
/*
* Check for empty case
*/
if (verify->text->ptr == NULL || verify->text->length == 0) {
verify->text->ptr = XtNewString("");
return;
}
/*
* Check for invalid characters
*/
len = verify->text->length;
newStr = (char*)XtCalloc(len + 10, sizeof(char));
(void)strncpy(newStr, verify->text->ptr, len);
for (i = 0, pos = verify->startPos; i < len; i++, pos++) {
ch = newStr[i];
if (!isdigit((int)ch) && ch != '-' && ch != '+') {
verify->doit = False;
break;
}
if ((ch == '-' || ch == '+') && pos) {
verify->doit = False;
break;
}
}
/*
* If a range was specified, use its signs to determine if '+' or
* '-' should be allowed
*/
if (range && verify->doit && len) {
if (range->min > range->max)
verify->doit = False;
else if (range->min >= 0 && strchr(newStr, '-'))
verify->doit = False;
else if (range->max < 0 && strchr(newStr, '+'))
verify->doit = False;
}
XtFree((char*)newStr);
}
/**************************************************************************
*
* Function: intValueCB
*
* Description: Verifies that the text field entry falls within the specified
* range. If a value is out of range the closest point in the range
* replaces the entered value.
*
* Parameters:
* w (I) - widget which triggered this callback
* clientData (I) - min/max value struct
* callData (I) - any callback
*
* Return: none
*
**************************************************************************/
/* ARGSUSED */
void intValueCB(Widget w, XtPointer clientData, XtPointer callData)
{
Irange *range = (Irange*)clientData;
char *str, buf[50];
int val;
str = XmTextFieldGetString(w);
if (*str) {
val = atoi(str);
if (val < range->min) {
(void)sprintf(buf, "%d", range->min);
XmTextFieldSetString(w, buf);
} else if (val > range->max) {
(void)sprintf(buf, "%d", range->max);
XmTextFieldSetString(w, buf);
}
}
XtFree((char*)str);
}
/**************************************************************************
*
* Function: floatVerifyCB
*
* Description: Used as a verify called for text fields which accept
* floating point values. The routine will disallow everything except
* an empty text field, the digits 0-9, the '.', or if the specified range
* permits, '-' and '+'. The numerical value of the input is not
* checked against the specified range. If the range structure pointer
* is NULL, no sign checking is performed.
*
* Parameters:
* w (I) - widget which triggered this callback
* clientData (I) - min/max value struct
* callData (I) - text verify callback
*
* Return: none
*
**************************************************************************/
/* ARGSUSED */
void floatVerifyCB(Widget w, XtPointer clientData, XtPointer callData)
{
register int i, len;
register char ch;
char *newStr;
Position pos;
XmTextVerifyCallbackStruct *verify = (XmTextVerifyCallbackStruct*)callData;
Frange *range = (Frange*)clientData;
/*
* Assume allowed
*/
verify->doit = True;
/*
* Check for empty case
*/
if (verify->text->ptr == NULL || verify->text->length == 0) {
verify->text->ptr = XtNewString("");
return;
}
/*
* Test for invalid characters
*
* XXX - should not allow multiple '.'
*/
len = verify->text->length;
newStr = (char*)XtCalloc(len + 10, sizeof(char));
(void)strncpy(newStr, verify->text->ptr, len);
for (i = 0, pos = verify->startPos; i < len; i++, pos++) {
ch = newStr[i];
if (!isdigit((int)ch) && ch != '-' && ch != '+' && ch != '.') {
verify->doit = False;
break;
}
if ((ch == '-' || ch == '+') && pos) {
verify->doit = False;
break;
}
}
/*
* Test for invalid '-' and '+'
*/
if (range && verify->doit && len) {
if (range->min > range->max)
verify->doit = False;
else if (range->min >= 0.0 && strchr(newStr, '-'))
verify->doit = False;
else if (range->max < 0.0 && strchr(newStr, '+'))
verify->doit = False;
}
XtFree((char*)newStr);
}
/**************************************************************************
*
* Function: findStr
*
* Description: Searches an array of strings for the specified string.
* Currently the search is linear.
*
* Parameters:
* array (I) - array of strings. Can be a structure with the
* incr parameter set appropriately.
* len (I) - number of entries in array.
* str (I) - string for which to search.
* incr (I) - amount to increment through the array. This allows us
* to pass arrays of structures and access the string
* members.
*
* Return: Returns the array index of the matched string if found or
* -1 if not found.
*
**************************************************************************/
int findStr(char **array, int len, char *str, int incr)
{
register int i, offset, pos = -1;
for (i = 0, offset = 0; i < len; i++, offset += incr) {
if (!strcmp(*(char**)((paddr_t)array + offset), str)) {
pos = i;
break;
}
}
return pos;
}
/**************************************************************************
*
* Function: buildOpts
*
* Description: Builds an options string. This is nothing more than a
* routine which appends the specified string onto the end of a
* given string. A space separates the origanl string and the appended
* string. This routine allocates storage for the new string.
*
* Parameters:
* origStrp (O) - pointer to the original string. Must point to
* NULL the first time through.
* newStr (I) - string to append to origStrp.
*
* Return: none
*
**************************************************************************/
void buildOpts(char **origStrp, const char *newStr)
{
register int len, origLen;
if (!*origStrp)
*origStrp = XtNewString(newStr);
else {
origLen = strlen(*origStrp);
len = origLen + strlen(newStr) + 5;
*origStrp = (char*)XtRealloc(*origStrp, len);
(void)sprintf(*origStrp + origLen, " %s", newStr);
}
}
/**************************************************************************
*
* Function: makeOpt
*
* Description: Constructs a string consisting of a '-', an option switch
* and any arguments that the switch might take.
*
* Parameters:
* name (I) - name of option switch as it should appear on a printer
* specific option command-line.
* args... (I) - parameter list of arguments, Must be NULL terminated.
*
* Return: String for the option. Storage is allocate by the function
* and should be freed by the caller when no longer needed. Free
* using the XtFree function.
*
**************************************************************************/
/* VARARGS */
char *makeOpt(char *name, ...)
{
va_list args;
char *optStr, *aptr;
optStr = (char*)XtMalloc(strlen(name) + 5);
(void)sprintf(optStr, "-%s", name);
va_start(args, name);
while ((aptr = (char*)va_arg(args, char*)) != NULL) {
optStr = (char*)XtRealloc(optStr, strlen(optStr) + strlen(aptr) + 5);
(void)strcat(optStr, " ");
(void)strcat(optStr, aptr);
}
va_end(args);
return optStr;
}
/**************************************************************************
*
* Function: setTextFSensitivity
*
* Description: Sets the sensitivity of the specified text field widget
* and changes the widget's background to the appropriate color.
*
* Parameters:
* widget (I) - ID of text field widget
* state (I) - True if sensitive, False otherwise
*
* Return: none
*
**************************************************************************/
void setTextFSensitivity(Widget widget, Boolean state)
{
static Boolean firstTime = True;
static GUIColor sensColor;
static GUIColor insensColor;
static Colormap colormap;
GUIColor *color;
DEFARGS(5);
/*
* We only need to do this first part once, when we are
* first called. We need to determine what the sensitive
* background color is and determine shadow colors for
* both the sensitive and insensitive cases.
*/
if (firstTime) {
/*
* Get the sensitive background color of a representative
* field widget
*/
STARTARGS;
SETARG(XmNforeground, &sensColor.foreground);
SETARG(XmNbackground, &sensColor.background);
SETARG(XmNcolormap, &colormap);
XtGetValues(widget, ARGLIST);
/*
* Compute the text field shadow colors
* for the sensitive and insensitive states
*/
XmGetColors(XtScreen(widget), colormap, sensColor.background, NULL,
&sensColor.tshadow, &sensColor.bshadow, NULL);
insensColor.foreground = gmodelResources.insensitiveFore;
insensColor.background = gmodelResources.insensitiveBack;
XmGetColors(XtScreen(widget), colormap, insensColor.background, NULL,
&insensColor.tshadow, &insensColor.bshadow, NULL);
firstTime = False;
}
/*
* Set the text field sensitivity and set the
* appropriate colors
*/
XtSetSensitive(widget, state);
color = (state)? &sensColor: &insensColor;
STARTARGS;
SETARG(XmNforeground, color->foreground);
SETARG(XmNbackground, color->background);
SETARG(XmNtopShadowColor,color->tshadow);
SETARG(XmNbottomShadowColor, color->bshadow);
XtSetValues(widget, ARGLIST);
}
/**************************************************************************
*
* Function: setListSensitivity
*
* Description: Sets the sensitivity of the specified list widget
* and changes the widget's foreground to the appropriate color.
*
* Parameters:
* widget (I) - ID of list widget
* state (I) - True if sensitive, False otherwise
*
* Return: none
*
**************************************************************************/
void setListSensitivity(Widget widget, Boolean state)
{
static Boolean firstTime = True;
static GUIColor sensColor;
static GUIColor insensColor;
GUIColor *color;
DEFARGS(5);
/*
* We only need to do this first part once, when we are
* first called. We need to determine what the sensitive
* background color is and determine shadow colors for
* both the sensitive and insensitive cases.
*/
if (firstTime) {
/*
* Get the sensitive colors of a sensitive widget
*/
STARTARGS;
SETARG(XmNforeground, &sensColor.foreground);
XtGetValues(widget, ARGLIST);
insensColor.foreground = gmodelResources.insensitiveFore;
firstTime = False;
}
/*
* Set the list sensitivity and set the appropriate colors
*/
XtSetSensitive(widget, state);
color = (state)? &sensColor: &insensColor;
STARTARGS;
SETARG(XmNforeground, color->foreground);
XtSetValues(widget, ARGLIST);
}
/**************************************************************************
*
* Function: getHalftoneScreens
*
* Description: Gets a list of screens from the resource file.
*
* Parameters:
*
* Return: number of halftones parsed
*
**************************************************************************/
int getHalftoneScreens(Widget parent, char *printername, char **halftoneLbl)
{
int count;
int len;
int ii;
char screenName[MAX_HALFTONE_CHAR_SIZE];
char *str;
char *tptr;
count = 0;
/* Get the screens common to all printers */
str=GetResourceString(parent,"CommonScreens", (char*) XtName(parent),"");
if (!str) return count;
/* The screen names are seperated by commas, parse em */
tptr=str;
while ((halftoneLbl[count] = strtok(tptr,",")) != NULL) {
tptr=NULL;
count++;
if (count == MAX_HALFTONE_SCREENS) return count ;
}
/* There can also be screens specific to a printer model. Get hose */
if (!printername) return count; /* Make sure this valid before using */
str=GetResourceString(parent,screenName, (char*) XtName(parent),"");
/* The resource name is the printer name with _Screens appended (printer
* name comes from the NAME= field in the model file. We replace blanks
* with underscores.
*/
(void) strncpy(screenName,printername,MAX_HALFTONE_CHAR_SIZE);
(void) strncat(screenName,"_Screens",
MAX_HALFTONE_CHAR_SIZE-strlen(screenName));
len=strlen(screenName);
for (ii=0;ii<len;ii++) {
if (screenName[ii]==' ') screenName[ii]='_';
}
/* Now get the screens and parse them adding to our list. */
str=GetResourceString(parent,screenName, (char*) XtName(parent),"");
tptr=str;
while ((halftoneLbl[count] = strtok(tptr,",")) != NULL) {
tptr=NULL;
count++;
if (count == MAX_HALFTONE_SCREENS) return count ;
}
return count;
}